﻿using HastyAPI.Nexmo;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using AuthorizeNet;

namespace Zatify.API
{
    public class PaymentService
    {
        public PaymentService()
        {
        }

        public static string AuthorizeNetLoginId
        {
            get { return ConfigurationManager.AppSettings.Get("AuthorizeNetLoginId"); }
        }

        public static string AuthorizeNetTransactionKey
        {
            get { return ConfigurationManager.AppSettings.Get("AuthorizeNetTransactionKey"); }
        }

        public static bool AuthorizeNetIsTestMode
        {
            get { return Convert.ToBoolean(ConfigurationManager.AppSettings.Get("AuthorizeNetIsTestMode")); }
        }

        public static bool AuthorizeNetEnabled
        {
            get { return Convert.ToBoolean(ConfigurationManager.AppSettings.Get("AuthorizeNetEnabled")); }
        }

        public static string AuthorizeNetDuplicateWindow
        {
            get { return ConfigurationManager.AppSettings.Get("AuthorizeNetDuplicateWindow"); }
        }

        public static bool Pay(string cardNumber, string cardPin, DateTime cardDateExpiration, decimal amount, string description, out string message)
        {
            if (!AuthorizeNetEnabled)
            {
                message = "";
                return true;
            }

            bool result = false;
            try
            {
                //Create the request
                var request = new AuthorizationRequest(cardNumber, cardDateExpiration.ToString("MMyy"), amount, description);

                //These are optional calls to the API
                if (!string.IsNullOrEmpty(cardPin))
                    request.AddCardCode(cardPin);
                if (!string.IsNullOrEmpty(AuthorizeNetDuplicateWindow))
                    request.DuplicateWindow = AuthorizeNetDuplicateWindow;

                //request.Queue("x_market_type", "2");
                //Customer info - this is used for Fraud Detection
                //request.AddCustomer("id", "first", "last", "address", "state", "zip");
                //order number
                //request.AddInvoice("invoiceNumber");
                //Custom values that will be returned with the response
                //request.AddMerchantValue("merchantValue", "value");
                //Shipping Address
                //request.AddShipping("id", "first", "last", "address", "state", "zip");


                //create the gateway, sending in your credentials and setting the Mode to Test (boolean flag)
                //which is true by default
                //this login and key are the shared dev account - you should get your own if you 
                //want to do more testing
                var gate = new Gateway(AuthorizeNetLoginId, AuthorizeNetTransactionKey, AuthorizeNetIsTestMode);

                //make payment
                var response = gate.Send(request);

                if (response.Approved)
                    result = true;
                message = response.Message;
            }
            catch (Exception ex)
            {
                message = ex.Message;
            }

            return result;
        }

    }
}
